package com.example.ovulationperiodtracker.Allactivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.ActivityMain2Binding;
import com.example.ovulationperiodtracker.fragment.CalendarFragment;
import com.example.ovulationperiodtracker.fragment.CalendarFragment2;
import com.example.ovulationperiodtracker.fragment.ChartFragment;
import com.example.ovulationperiodtracker.fragment.MainFragment;
import com.example.ovulationperiodtracker.fragment.MainFragment2;
import com.example.ovulationperiodtracker.fragment.MineFragment;
import com.example.ovulationperiodtracker.fragment.StatisticsFragment;
import com.example.ovulationperiodtracker.fragment.StatisticsFragment2;
import com.example.ovulationperiodtracker.model.Notifications;
import com.example.ovulationperiodtracker.model.Period;
import com.example.ovulationperiodtracker.model.Settings;
import com.example.ovulationperiodtracker.model.User;
import com.example.ovulationperiodtracker.servise.EvaAlarmReceiver;
import com.example.ovulationperiodtracker.utils.CustomTypefaceSpan;
import com.example.ovulationperiodtracker.utils.JCGSQLiteHelper;
import com.example.ovulationperiodtracker.utils.MainUiSetting;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.github.florent37.singledateandtimepicker.SingleDateAndTimePicker;
import com.irfaan008.irbottomnavigation.SpaceItem;
import com.irfaan008.irbottomnavigation.SpaceNavigationView;
import com.irfaan008.irbottomnavigation.SpaceOnClickListener;
import com.necer.base.BaseActivity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class MainActivity2 extends BaseActivity<ActivityMain2Binding> {


    @Override
    protected ActivityMain2Binding createBinding(LayoutInflater inflater) {
        return ActivityMain2Binding.inflate(inflater);
    }

    @Override
    protected void onCreated(Bundle savedInstanceState) {

        List<Fragment> fragmentList = new ArrayList<>();
     //   fragmentList.add(new CalendarFragment());
        fragmentList.add(new CalendarFragment2());
        fragmentList.add(new ChartFragment());
     //   fragmentList.add(new MainFragment());
        fragmentList.add(new MainFragment2());
        fragmentList.add(new StatisticsFragment2());
        fragmentList.add(new MineFragment());

        MainUiSetting.setMainTab(binding.viewPager, binding.tabLayout, fragmentList, this);

        setScanWith();



        binding.llScan.setOnClickListener(v -> {
            binding.tabLayout.setCurrentTab(2);
            binding.viewPager.setCurrentItem(2, false);
        });


        //默认显示第三个Fragment
        setCheckFragment(2);


    }



    private void setScanWith() {
        WindowManager windowManager = getWindowManager();
        int screenWidth;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
            screenWidth = getWindowManager().getCurrentWindowMetrics().getBounds().width();
        } else {
            screenWidth = windowManager.getDefaultDisplay().getWidth();
        }
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) binding.llScan.getLayoutParams();
        params.width = screenWidth / 5 + 100;
        binding.llScan.setLayoutParams(params);
    }


    //切换Fragment
    public void setCheckFragment(int index) {
        binding.viewPager.post(new Runnable() {
            @Override
            public void run() {

                binding.tabLayout.setCurrentTab(index);
                binding.viewPager.setCurrentItem(index, false);
            }
        });
    }


    private long backPressedTime;
    private Toast exitToast;
    @Override
    public void onBackPressed() {
        if (backPressedTime + 2000 > System.currentTimeMillis()) {
            if (exitToast != null) {
                exitToast.cancel();
            }
            super.onBackPressed();
            return;
        }
        backPressedTime = System.currentTimeMillis();
        exitToast = Toast.makeText(getApplicationContext(), "再按一次返回键退出应用", Toast.LENGTH_SHORT);
        exitToast.show();
    }
}
